// pages/camera/camera.js
const serviceUrl = getApp().globalData.serviceUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '',
    show: false,
    idCardInfo: null,
  },
  
  uploadIdCardImage(tempImagePath)  {
    let that =this;
    let token = wx.getStorageSync('token');

    wx.uploadFile({
      url: serviceUrl + '/api/ImageOcr/PostIdentityCard',
      header: {
        'Authorization': 'Bearer ' + token
      },
      filePath: tempImagePath,
      name: 'file',
      success(success) {
		 let {statusCode,data} = success;
		 if(statusCode == 200)
		 {
			 let successobj = JSON.parse(success.data);
			 const { resultState, resultMes, result } = successobj;
			 console.log(resultState, resultMes, result);
			 if (resultState == 1) {
			   that.setData({
			     idCardInfo: result,
			     src: tempImagePath,
			     show: true
			   })
			 } else {
			   wx.showModal({
			     title: '识别失败',
			     content: '身份证识别失败'
			   });
			 }
		 }else{
			 wx.showModal({
			   title: '识别失败',
			   content: data
			 });
		 }
       
        //do something
        wx.hideLoading();
      },
      fail(error) {
        console.log(error);
        wx.hideLoading();
      }
    })

  },

  returnBtn() {
    wx.navigateBack();
  },

  openImg() {
    let that = this;
    
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.showLoading();
        that.uploadIdCardImage(tempFilePaths[0]);
      }
    })
  },

  cancelBtn() {
    this.setData({ show: false })
  },

  saveImg() {

  
    // wx.showModal({
    //   title: '身份证信息',
    //   content: `${birthDate}-${gender}-${address}-${nationality}-${name}-${idNumber}`//JSON.stringify(this.data.idCardInfo),
    // })

    let pages = getCurrentPages();
    let beforePage = pages[pages.length-2]
  
    //console.log(beforePage);

    wx.navigateBack({
      success:()=>{
        beforePage.setPersonIdCard(this.data.idCardInfo)
      }
    });
  },

  takePhoto() {
    let that = this;
    //let token = wx.getStorageSync('token');
    const ctx = wx.createCameraContext()
    // const listener = ctx.onCameraFrame((frame) => {
    //   console.log(frame)
    // })
    ctx.takePhoto({
      quality: 'medium',
      success: (res) => {
        let  tempImagePath = res.tempImagePath;
        wx.showLoading();
        that.uploadIdCardImage(tempImagePath);
        // listener.stop({
        //   success: (res) => {
        //     console.log(res)
        //   },
        //   fail: (err) =>{
        //     console.log(err)
        //   }
        // })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
